package com.fitness.fitness.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fitness.fitness.mapper.RoleMapper;
import com.fitness.fitness.mapper.RoleUserMapper;
import com.fitness.fitness.modal.user.entity.Role;
import com.fitness.fitness.modal.user.entity.RoleUser;
import com.fitness.fitness.service.IRoleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {

    @Resource
    private RoleUserMapper roleUserMapper;

    @Override
    public List<String> getRoleList(Long uid) {
        List<Role> roles = this.baseMapper.getRoleList(uid);
        if(roles == null || roles.isEmpty()){
            return new ArrayList<>();
        }
        return roles.stream().map(Role::getName).collect(Collectors.toList());
    }

    @Override
    public List<Role> allRoles() {
        return this.baseMapper.selectList(null);
    }

    @Override
    public Role getRoleByUid(Long userId) {
        return this.baseMapper.getRoleByUid(userId);
    }

    @Override
    public Boolean saveRoleUser(RoleUser roleUser) {
        return roleUserMapper.insert(roleUser) > 0;
    }
}
